package com.fuluwa.infra.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;

import org.apache.log4j.Logger;

public class MD5Util {

	private static final Logger logger = Logger.getLogger(MD5Util.class);

	private static final String MD5 = "MD5";

	public static String encrypt(byte[] value) {
		if (value == null)
			return "";

		MessageDigest md = null;
		String strDes = null;

		try {
			md = MessageDigest.getInstance(MD5);
			md.update(value);
			strDes = HexUtil.bytes2Hex(md.digest());
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
		return strDes;
	}

	public static String md5Sign(Map<String, String> map, String key, String charset) {
		return md5Sign(map, "key", key, charset);
	}

	public static String md5Sign(Map<String, String> map, String keyName, String keyValue, String charset) {
		if (map == null || keyValue == null || keyValue.length() == 0) {
			return null;
		}

		ArrayList<String> lst = new ArrayList<String>();
		String s = "";
		for (String k : map.keySet()) {
			lst.add(k);
		}

		Collections.sort(lst, new Comparator<String>() {
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});

		for (int i = 0; i < lst.size(); i++) {
			String k = lst.get(i);
			String v = map.get(k);
			s += k + "=" + v + "&";
		}
		s += keyName + "=" + keyValue;
		logger.info("MD5 Src:" + s + " key:" + keyValue + "]");
		String sign = "";
		try {
			sign = encode2HexStr(s.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		logger.info("MD5 Result:" + sign);
		return sign;
	}

	private static byte[] encode(byte[] bytes) {
		MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.update(bytes);
			byte[] digesta = digest.digest();
			return digesta;
		} catch (Exception e) {
		}
		return null;
	}

	private static String encode2HexStr(byte[] bytes) {
		return HexUtil.bytes2HexStr(encode(bytes));
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String s = "abc";
		System.out.println(encode2HexStr(s.getBytes("UTF-8")));
		// HashMap<String, String> mp = new HashMap<String, String>();
		// mp.put("ver", "2.0");
		// mp.put("charset", "1");
		// mp.put("pay_result", "0");
		// mp.put("transaction_id", "1204527301201008209009346613");
		// mp.put("sp_billno", "20100820151913777");
		// mp.put("total_fee", "1");
		// mp.put("fee_type", "1");
		// mp.put("bargainor_id", "1204527301");
		// mp.put("attach", new String("583873140~qq三国".getBytes("gb2312"),
		// "gb2312"));
		// // mp.put("sign",sign);
		// mp.put("sign_sp_id", "1000000101");
		// String msign = MD5Util.md5SignwithKey(mp, "utf8");
		//
		// System.out.println("msign:" + msign);
		// String s = "[]583873140~qq三国";
		// System.out.println("s:" + StringUtil.removeInvalidWML(s));

		// String s1 = new String(s.getBytes("gb2312"),"gb2312");
		// System.out.println(HexUtil.bytes2HexStr(s.getBytes("gb2312")));
		//
		// System.out.println(new String(HexUtil.hexStr2Bytes(HexUtil
		// .bytes2HexStr(s.getBytes("gb2312"))), "gb2312"));
		// System.out.println(new String(HexUtil
		// .hexStr2Bytes("3538333837333134307E7171C8FDB9FA"), "gb2312"));
		// 18F0E558F90854029B4EE7238934AD3F

		/*
		 * String s =
		 * "attach=583873140~qq三国&bargainor_id=1204527301&charset=1&fee_type=1&pay_result=0&sign_sp_id=1000000101&sp_billno=201008201242294437&total_fee=1&transaction_id=1204527301201008209009332710&ver=2.0&key=f4becb5fb2e007c5fb7238b619b8956b"
		 * ; String s1 = new String(s.getBytes("UTF-8"),"gb2312"); String sign =
		 * MD5Coding.encode2HexStr(s1.getBytes());
		 * System.out.println(s.getBytes(
		 * "UTF-8").length+"|"+s1.getBytes("UTF-8").length+"|"+sign);
		 */
	}
}
